package com.roadjava.GDOUGym.service.impl;


import com.roadjava.GDOUGym.bean.dto.GameDTO;
import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.entity.EquipmentDO;
import com.roadjava.GDOUGym.bean.entity.GameDO;
import com.roadjava.GDOUGym.bean.entity.PlaceDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.convert.GameConvert;
import com.roadjava.GDOUGym.convert.SiteConvert;
import com.roadjava.GDOUGym.mapper.GameMapper;
import com.roadjava.GDOUGym.mapper.SiteMapper;
import com.roadjava.GDOUGym.service.GameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class GameServiceImpl implements GameService {
    @Resource
    private GameMapper gameMapper;

    @Resource
    private GameConvert gameConvert;

    @Override
    public ResultDTO<List<GameDTO>> find(GameDTO dto) {
        GameDO gameDO = gameConvert.dto2do(dto);
        System.out.println(gameDO);
        List<GameDO> dos = gameMapper.find(dto);
        List<GameDTO> dtos = gameConvert.dos2dtos(dos);
        long total = gameMapper.selectCount(dto);
        return ResultDTO.buildSuccess(dtos,total);
    }

    @Override
    public ResultDTO<String> add(GameDTO gameDTO) {
        GameDTO dto = new GameDTO();
        String no;
        List<GameDO> gameDOList = gameMapper.find(dto);
        if(gameDOList.size() == 0){
            no ="0001";
        }else{
            String endNo = gameDOList.get(gameDOList.size()-1).getContesNo();
            long total = Integer.parseInt(endNo)+1;
            no= "";
            if(total>=0 && total<10){
                no = "000"+total;
            }else if(total>=10 && total<100){
                no = "00"+total;
            }else if(total>=100 && total<1000){
                no = "0"+total;
            }else{
                no = total+"";
            }
        }

        // 把dto转为do对象
        GameDO gameDO = gameConvert.dto2do(gameDTO);

        gameDO.setContesNo(no);
        gameDO.setContesState("审核通过");
        int result = gameMapper.add(gameDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("添加成功");
        }else {
            return ResultDTO.buildSuccess("添加失败");
        }
    }

    @Override
    public ResultDTO<String> change(GameDTO gameDTO) {
        // 把dto转为do对象
        GameDO gameDO = gameConvert.dto2do(gameDTO);
        int result = gameMapper.change(gameDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<String> delete(GameDTO gameDTO) {
        // 把dto转为do对象
        GameDO gameDO = gameConvert.dto2do(gameDTO);
        int result = gameMapper.delete(gameDO);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }
}
